/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.Constants;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.model.Menu;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.pages.BackPage;
import com.legendshop.oa.pages.FowardPage;
import com.legendshop.oa.pages.RedirectPage;
import com.legendshop.oa.security.UserManager;
import com.legendshop.permission.service.MenuManager;
import com.legendshop.util.AdminMenuUtil;
import com.legendshop.util.CookieUtil;

/**
 * 后台首页功能.
 */
@Controller
public class IndexAdminController extends BaseController {
	/** The log. */
	private final Logger log = LoggerFactory.getLogger(IndexAdminController.class);
	
	@Autowired
	private MenuManager menuManager;
	
	@Autowired
	private SessionRegistry adminSessionRegistry;
	
	/**
	 * 首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = {"/"})
	public String index(HttpServletRequest request, HttpServletResponse response) {
		if(UserManager.isUserLogined(request.getSession())){//已经登录的
			return PathResolver.getPath(request, response, FowardPage.PROJECT_QUERY);
		}else {
			return PathResolver.getPath(request, response, RedirectPage.ADMIN_LOGIN);
		}
	}
	
	
	/**
	 * 记住当前选择的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = {"/admin/index"})
	public String home(HttpServletRequest request, HttpServletResponse response) {
		AdminMenuUtil.parseMenu(request, 0); //固定死选择第一个菜单
		CookieUtil.deleteCookie(request, response, "_ls_menu_");//重新进入， 清空cookies中的当前选中菜单，默认选中第一个。see legendshop.js
		
		/** 在线用户  **/
		int onLineUser = adminSessionRegistry.getAllPrincipals().size();
		request.setAttribute("onLineUser",onLineUser);
		return PathResolver.getPath(request, response, FowardPage.PROJECT_QUERY);
	}
	
	/**
	 * 菜单
	 * @param request
	 * @param response
	 * @param order
	 * @return
	 */
	@RequestMapping("/admin/menu/{order}")
	public String menu(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer order) {
			Menu currentMenu = AdminMenuUtil.parseMenu(request, order); //固定死选择某个个菜单
			 
			 //记录三级菜单的action
			 String menuAction = null;
				 if(currentMenu!=null && currentMenu.getSubList() != null && currentMenu.getSubList().get(0).getSubList() != null){
					 Menu thirdMenu =currentMenu.getSubList().get(0).getSubList().get(0);
					 menuAction = thirdMenu.getAction();
				 }
				 
				 if(menuAction == null){//返回首页
					 return PathResolver.getPath(request, response,  RedirectPage.ADMIN_INDEX_QUERY);
				 }
			 
			 return PathResolver.getPath(request, response, menuAction, RedirectPage.VARIABLE);
		 
	}
	
	/**
	 * 显示左边的菜单
	 * @param request
	 * @param response
	 * @param order
	 * @return
	 */
	@RequestMapping("/admin/menuContent/{order}")
	public String menuContent(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer order) {
		AdminMenuUtil.parseMenu(request, order); //固定死选择某个个菜单
		 return PathResolver.getPath(request, response, BackPage.MENU);
	}
	
	/**
	 * 显示顶部的菜单
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/top")
	public String top(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		//加载菜单
		try {
			 menuManager.calUserMenus(session);
		} catch (Exception e) {
			session.removeAttribute(Constants.MENU_LIST);
			log.error("Calculate admin menu error ", e );
		}
		return PathResolver.getPath(request, response, BackPage.ADMIN_TOP);
	}

	/**
	 * 后台登陆页面.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @return the string
	 */
	@RequestMapping("/adminlogin")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		String path = PathResolver.getPath(request, response, BackPage.ADMIN_LOGIN);
		return path;
	}
	
	/**
	 * 错误页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/404")
	public String error404(HttpServletRequest request, HttpServletResponse response) {
		return PathResolver.getPath(request, response, AdminTilesPage.ADMIN_404);
	}
	

}
